//
//  RHPictureView.swift
//  微博新浪
//
//  Created by hezerong on 16/6/17.
//  Copyright © 2016年 Rocky. All rights reserved.
//

import UIKit

private let numberOfRow = 3

class RHPictureView: UIView {
    
    var viewModel:RHStatusViewModel?{
    
        didSet{
            
              //先把所有图片视图全部隐藏
            for imageView in subviews{
            
                imageView.hidden = true
            }
            
            //定义下标变量
            var index = 0
            
            //遍历pic_urls数组，例如：pic_url有5张图片，则将前面5个imageView显示出来
            if let pic_urls = viewModel?.statusModel.pic_urls{
            
                for pictureModel in pic_urls{
                
                    //给imag设置完整图片
                    let imageView = subviews[index++] as! UIImageView
                    imageView.setImage(pictureModel.thumbnail_pic, placeHolder: "avatar_default_big")
                    //处理一共有四张图片的情况，如果一共有四张图片，则直接跳过第三张图片。index ＝＝ 2时，index++会直接跳过第三张图片
                    
                    if pic_urls.count == 4 && index == 2 {
                    
                       index++
                    }
                    
                    //如果有图片，则将imagview显示
                    imageView.hidden = false
                }
                
                //修改单张图片的size
                updatingSingleImageView()
            }
        }
    }
    
    var pic_urls:[RHPictureModel]?

    init(){
    
        super.init(frame: CGRectZero)
        
        setUpUi()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}


extension RHPictureView{
    
    //修改单张图片的尺寸
    func updatingSingleImageView(){
    
         let firstImageView = subviews[0]
         //1.判断pic_urls是不是只有一张图片
        if let picUrls = viewModel?.pic_urls where picUrls.count == 1{
            //如果有一张图片，更新图片的frame
            firstImageView.frame.size = (viewModel?.singlePictureViewSize)!
        
        }
        else{
        
        
            //3.如果没有图片，或者有多张图片，就把imageView的frame设为初始值。
            firstImageView.frame.size = CGSizeMake(cellLayout_constant.singlePictureWidth, cellLayout_constant.singlePictureWidth)
        }
    }

    func setUpUi(){
    
        //设置背景颜色
        backgroundColor = UIColor.greenColor()
        
        //图片视图的大小之前已经计算，如果图片视图小于三行高度，则以下添加的子视图会超出图片视图，将以下属性设为true，则超出的部分不显示。
         clipsToBounds = true
        
        //横向或纵向步进
        let step = cellLayout_constant.separator + cellLayout_constant.singlePictureWidth
        
        //第一张图片的rect
        let rect = CGRect(origin: CGPointZero, size: CGSizeMake(cellLayout_constant.singlePictureWidth, cellLayout_constant.singlePictureWidth))
        
        //向图片视图添加9个imageview
        for i in 0..<9{
            
            let row = i / numberOfRow
            let column = i % numberOfRow
            
            
            let rect = CGRectOffset(rect, step * CGFloat(column), step * CGFloat(row))
            let imageView = UIImageView(frame: rect)
            
            imageView.clipsToBounds = true
            
            imageView.backgroundColor = UIColor.redColor()
            
            addSubview(imageView)
            
            imageView.tag = i
            imageView.userInteractionEnabled = true
            
            
            
            //添加点击事件
            let tapGesture = UITapGestureRecognizer(target: self, action: "imageViewDidSelect:")
            imageView.addGestureRecognizer(tapGesture)

        }
        
    }
}

extension RHPictureView{


    func imageViewDidSelect(tap:UITapGestureRecognizer){
    
        if var selectedIndex = tap.view?.tag{
        
            //获取当的被点击的图片的索引indexa
            if selectedIndex > 2 && viewModel?.pic_urls?.count == 4 {
              
                selectedIndex -= 1
            }
            
            //获取所有的urls的值的数组
            let urls = ((viewModel?.pic_urls)! as NSArray).valueForKey("bmiddle_pic")
            
            let userInfo = [selectedNotificationStrut.selectedIndexKey:selectedIndex , selectedNotificationStrut.urlKey:urls]
            
            let notification = NSNotification(name: selectedNotificationStrut.notificationName, object: self, userInfo: userInfo)

            NSNotificationCenter.defaultCenter().postNotification(notification)
            
            print(urls)
            print("taps\(selectedIndex)")
        }
    }
}